package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_template;

import cn.com.api.service.ApiU_templateService;

import cn.com.util.*;


public class U_templateBaseController {

	private static Logger logger = Logger.getLogger(U_templateBaseController.class);
	
	@Autowired
	private ApiU_templateService apiU_templateService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_template-list")
	public ModelAndView u_template_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_template_list.jsp");
		
		List<ApiU_template> u_templates = apiU_templateService.findU_templates();
		mv.addObject("u_templates", u_templates);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_template-add-pre")
	public ModelAndView u_template_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_template_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_template u_template = apiU_templateService.findU_templateById(Integer.valueOf(id));
			mv.addObject("u_template",u_template);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_template-add")
	public void u_template_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String template_id = request.getParameter("template_id");
		String tpl_name = request.getParameter("tpl_name");
		String tpl_thumb = request.getParameter("tpl_thumb");
		String bg_image = request.getParameter("bg_image");
		String bg_audio = request.getParameter("bg_audio");
		String type = request.getParameter("type");
		String tpl_desc = request.getParameter("tpl_desc");
		String tpl_level = request.getParameter("tpl_level");
		String create_time = request.getParameter("create_time");
		String create_by = request.getParameter("create_by");
		String modify_time = request.getParameter("modify_time");
		String modify_by = request.getParameter("modify_by");
		String status = request.getParameter("status");
		String controller_route = request.getParameter("controller_route");
		String theme = request.getParameter("theme");
		String parameters = request.getParameter("parameters");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_template u_template = new ApiU_template();
            /*
		    u_template.setTemplate_id(template_id);
		    u_template.setTpl_name(tpl_name);
		    u_template.setTpl_thumb(tpl_thumb);
		    u_template.setBg_image(bg_image);
		    u_template.setBg_audio(bg_audio);
		    u_template.setType(type);
		    u_template.setTpl_desc(tpl_desc);
		    u_template.setTpl_level(tpl_level);
		    u_template.setCreate_time(create_time);
		    u_template.setCreate_by(create_by);
		    u_template.setModify_time(modify_time);
		    u_template.setModify_by(modify_by);
		    u_template.setStatus(status);
		    u_template.setController_route(controller_route);
		    u_template.setTheme(theme);
		    u_template.setParameters(parameters);
		    u_template.setExt_1(ext_1);
		    u_template.setExt_2(ext_2);
		    u_template.setExt_3(ext_3);
		    u_template.setExt_4(ext_4);
		    u_template.setExt_5(ext_5);
	        */
			apiU_templateService.insertU_template(u_template);
		}
		//edit
		else{
			ApiU_template u_template = apiU_templateService.findU_templateById(Integer.valueOf(id));
	        /*
		    u_template.setTemplate_id(template_id);
		    u_template.setTpl_name(tpl_name);
		    u_template.setTpl_thumb(tpl_thumb);
		    u_template.setBg_image(bg_image);
		    u_template.setBg_audio(bg_audio);
		    u_template.setType(type);
		    u_template.setTpl_desc(tpl_desc);
		    u_template.setTpl_level(tpl_level);
		    u_template.setCreate_time(create_time);
		    u_template.setCreate_by(create_by);
		    u_template.setModify_time(modify_time);
		    u_template.setModify_by(modify_by);
		    u_template.setStatus(status);
		    u_template.setController_route(controller_route);
		    u_template.setTheme(theme);
		    u_template.setParameters(parameters);
		    u_template.setExt_1(ext_1);
		    u_template.setExt_2(ext_2);
		    u_template.setExt_3(ext_3);
		    u_template.setExt_4(ext_4);
		    u_template.setExt_5(ext_5);
			*/
			apiU_templateService.updateU_template(u_template);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_template-delete")
	public void u_template_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_templateService.deleteU_template(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



